package com.juicygames.undoer.world;

import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.juicygames.framework.GameAction;
import com.juicygames.framework.action.DrawAction;
import com.juicygames.framework.action.TouchAction;
import com.juicygames.framework.action.UpdateAction;
import com.juicygames.framework.android.AndroidGame;
import com.juicygames.framework.draw.DrawService;
import com.juicygames.framework.gamepart.GamePart;
import com.juicygames.undoer.ballcounter.BallCounter;
import com.juicygames.undoer.fps.FpsDisplayer;
import com.juicygames.undoer.player.Player;

public class World extends GamePart implements AndroidGame {
	private static final String LOGTAG = World.class.getName();
	
	Player player;
	DrawService drawService;
	
	public World() {
		super(null);
	}

	public void load(Context context) {
		drawService = addPart(new DrawService(this));
		
		addPart(new FpsDisplayer(this));
		addPart(new BallCounter(this));
		
		player = new Player(this);
		addPart(player);
		player.load();
	}
	
	public void update(float timeElapsed, float framesPerSecond, Canvas canvas) {
		
		UpdateAction update = UpdateAction.getInstance(timeElapsed, framesPerSecond);
		update.apply(this);

		canvas.drawRGB(0, 0, 0);
		DrawAction draw = DrawAction.getInstance(canvas);
		draw.apply(drawService);
	}

	public void onKeyDown(int key) {
		if (key == KeyEvent.KEYCODE_D) {
			Log.i(LOGTAG,"This would be a neat place for a breakpoint");
		}
	}

	public void onKeyUp(int key) {
	}
	
	public void onTouch(MotionEvent event) {
		GameAction touchAction = TouchAction.getInstance(event);
		player.handleAction(touchAction);
	}
	
	public static int SCREEN_WIDTH = 320;
	public static int SCREEN_HEIGHT = 480;

}
